clear;

fileID = fopen('log_creatMDF.txt','w');




signallist = readtable("signalList_definition.xlsx",'VariableNamingRule','preserve');

[file,path] = uigetfile({'*.csv;*.xlsx','excelFile(*.csv;*.xlsx)'});
addpath(path)
excelTable = readtable(file,'VariableNamingRule' ,'preserve');
TotalSignalNum = max(size(signallist.SignalName_RAW));


fprintf(fileID,"filename:%s : %s\n",path,file);
fprintf(fileID,"-------------\n");
fprintf(fileID,"signal num:%d\n",int32(TotalSignalNum));

fprintf("filename:%s : %s\n",path,file);
fprintf("-------------\n");
fprintf("signal num:%d\n",int32(TotalSignalNum));

%%% read excel raw data
for i = 1:TotalSignalNum

    % fprintf("%d:%s,%s,%d\n",...
    %     int32(i),...
    %     string(signallist.SignalName_CN(i)),...
    %     string(signallist.SignalName_EN(i)),...
    %     int32(signallist.coding_convert(i))...
    %     );

    excelTable_NameCN_index = string(signallist.SignalName_RAW(i));
    excelTable_NameEN_index_ = string(signallist.SignalName_EN(i));
    % excelTable_NameEN_temp = strcat(excelTable_NameEN_index_,'_temp');
    % string1 = strcat(excelTable_NameEN_temp,' = excelTable.("',excelTable_NameCN_index,'");');
    % eval(string1);

    %.............................................................
    Is_ConvrtCN2Num = signallist.coding_convert(i);
    if Is_ConvrtCN2Num ~=1
        string2 = strcat(excelTable_NameEN_index_,' = excelTable.("',excelTable_NameCN_index,'");');
        eval(string2); %No handle enumCN Replace
        fprintf(fileID,'%d, EN>>EN:%s,%s\n',int16(i),excelTable_NameCN_index,excelTable_NameEN_index_);
        fprintf('%d, EN>>EN:%s,%s\n',int16(i),excelTable_NameCN_index,excelTable_NameEN_index_);
    end
    %
    %
    if Is_ConvrtCN2Num == 1
        excelTable_NameEN_temp = strcat(excelTable_NameEN_index_,'_MDF_temp5645442');
        string1 = strcat(excelTable_NameEN_temp,' = excelTable.("',excelTable_NameCN_index,'");');
        eval(string1);
        try
            string11 = strcat('signalPointsNums = size(',excelTable_NameEN_temp,',1);');
            eval(string11)
            % signalPointsNums = size(excelTable_NameEN_temp,1);
            excelTable_NameEN_index = -65531*ones(signalPointsNums,1);
            coding_raw = string(signallist.coding(i));
            coding_raw_spilt = strsplit(coding_raw,';');
            ENUM_Num = size(coding_raw_spilt,2);

            for i2 = 1:signalPointsNums
                for i1 = 1:ENUM_Num
                    codingPart12 = strsplit(coding_raw_spilt(i1),':');
                    codingPart1_Num = codingPart12(1);
                    codingPart2_Letter = codingPart12(2); %% get enumCN definition
                    string12 = strcat('signalCN_TEMP = ',excelTable_NameEN_temp,'(i2);');
                    eval(string12);
                    if string(signalCN_TEMP)==codingPart2_Letter
                        excelTable_NameEN_index(i2)= str2double(codingPart1_Num);
                        break
                    end
                end
            end
            fprintf(fileID,'%d,trans CN>>EN:%s,%s\n',int16(i),excelTable_NameCN_index,excelTable_NameEN_index_);
            fprintf('%d,trans CN>>EN:%s,%s\n',int16(i),excelTable_NameCN_index,excelTable_NameEN_index_);
        catch
            fprintf(fileID,'%d,xxxxXXXXX failed trans CN>>EN:%s\n',int16(i),excelTable_NameCN_index);
            fprintf('%d,xxxxXXXXX failed trans CN>>EN:%s\n',int16(i),excelTable_NameCN_index);
        end

        string13 = strcat(excelTable_NameEN_index_ ,'= excelTable_NameEN_index;'); % replace enumCN to Num
        eval(string13)

    end

end
clear *_MDF_temp5645442
fprintf(fileID,'trans done----------------------------------\n');

fclose(fileID);
